#!/usr/bin/python

#Ganna Raboshchuk {ganna.raboshchuk@upc.edu}
#Updated: 24.12.2014
#UPC, Barcelona

#Creates reference block based labels.
#--------------------------------------------------------------------------

import os.path
import math
import sys

#------------------------------------------------------------------------------
#Global variables
CORPUS = ['S3', 'S4', 'S11', 'S12', 'S13', 'S14', 'S15', 'S16', 'S17', 'S18']
SCENARIOS = ['SC1', 'SC2', 'SC3', 'SC4', 'SC5', 'SC6', 'SC7', 'SC8', 'SC9', 'SC10']
EXTRAS = [ '', '_2' ]
TIERS = [ 'alarms' ]

#Indexes of labels
START_TIME = 0
END_TIME = 1
COUNTS = 2
PARTIALS = 3

evaluation_interval = sys.argv[1] #in seconds
evaluation_interval = int(evaluation_interval)
resultsFolder = sys.argv[2]
class_of_interest = sys.argv[3]

#------------------------------------------------------------------------------
for session in CORPUS:
    for scenario in SCENARIOS:
        for extra in EXTRAS:
            for tier in TIERS:
                path = resultsFolder + 'evaluation_interval_' + str(evaluation_interval) + '/' + session + '/' + session + '_' + scenario + extra + '.txt'
                if not os.path.isfile(path):
                    continue
                else:
                    inputFile = open(path, 'r')
                    outputFile = open(resultsFolder + '/' + 'evaluation_interval_' + str(evaluation_interval) + '/' + session + '/' + session + '_' + scenario + extra + '.csv', 'w')
                    with open(path) as fileID:
                        for line in fileID:
                            interval = line.split('\t')
                            if (int(interval[COUNTS]) > 1):
                                outputFile.write(str(interval[START_TIME]) + ',' + str(interval[END_TIME]) + ',' + 'al' + '\n')
                            elif (int(interval[COUNTS]) >= 1 and int(interval[PARTIALS]) > 0):
                                outputFile.write(str(interval[START_TIME]) + ',' + str(interval[END_TIME]) + ',' + 'al' + '\n')
                            else:
                                outputFile.write(str(interval[START_TIME]) + ',' + str(interval[END_TIME]) + ',' + 'ot' + '\n')
inputFile.close()
outputFile.close()
